package com.lzx.iteam;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.SalaryListAdapter;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.DateData;
import com.lzx.iteam.bean.SalaryListBean;
import com.lzx.iteam.bean.SalaryRuleBean;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetSalaryListDataMsg;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.net.WithoutObjMsg;
import com.lzx.iteam.util.AllDialogUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2017年 01月 18日
 * Time: 下午2:53
 *
 * @QQ : 1234567890
 * **********************************
 *
 * 工资单页面
 */
public class SalaryListActivity extends MyBaseActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener{

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.top_layout_listview_only)
    private RelativeLayout mLayoutTop;
    @FindViewById(id = R.id.listview_only)
    private ListView mListView;

    private SalaryListAdapter mAdapter;
    private ArrayList<SalaryListBean> mData;
    private SalaryRuleBean mRuleData;

    private String mGroupId;
    private int mPosition = -1;
    private final int GET_SALARY_LIST = 30001;
    private final int DELETE_ONE_SALARY = 30002;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            waitDlgDismiss();
            if (msg.arg1 == 0){
                switch (msg.what){
                    case GET_SALARY_LIST:
                        mData = (ArrayList<SalaryListBean>) msg.obj;
                        mAdapter = new SalaryListAdapter(mActivity,mData);
                        mListView.setAdapter(mAdapter);
                        break;
                    case DELETE_ONE_SALARY:
                        mData.remove(mPosition);
                        mAdapter.notifyDataSetChanged();
                        break;
                }
            }else{
                Toast.makeText(mActivity, (String) msg.obj,
                      Toast.LENGTH_LONG).show();
            }

        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.only_list_layout);
    }

    @Override
    public void initData() {
        mTvTitle.setText("工资表");
        mLayoutTop.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("group_id");
        mRuleData = (SalaryRuleBean) intent.getSerializableExtra("salary_rule");
        getSalaryList();
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_ll_back:
                finish();
                break;
        }
    }

    /**
     * 获取生成的所有工资单
     */
    private void getSalaryList(){
        waitDlgShow();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        GetSalaryListDataMsg msg = new GetSalaryListDataMsg(mHandler.obtainMessage(GET_SALARY_LIST));
        msg.mApi = AsynHttpClient.API_SALARY_LIST_ALL;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }

    /**
     * 删除工资单
     */
    private void deleteOneSalaryForm(String salary_list_id){
        waitDlgShow();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair("salary_list_id", salary_list_id));
        WithoutObjMsg msg = new WithoutObjMsg(mHandler.obtainMessage(DELETE_ONE_SALARY));
        msg.mApi = AsynHttpClient.API_SALARY_DELETE;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String[] dateString = mData.get(position).getMonth().split("_");
        DateData date = new DateData();
        date.setYear(Integer.parseInt(dateString[0]));
        date.setMonth(Integer.parseInt(dateString[1]));


        Intent form = new Intent(mActivity,SalaryCalculateActivity.class);
        form.putExtra("action_type",1);
        form.putExtra("group_id",mGroupId);
        form.putExtra("salary_rule",mRuleData);
        form.putExtra("salary_id",mData.get(position).get_id());
        form.putExtra("month",date);
        startActivity(form);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        mPosition = position;
        showDeleteDlg(mData.get(position).get_id());
        return true;
    }

    /**
     * 取消设置
     */
    public void showDeleteDlg(final String salary_list_id) {
        AllDialogUtil mDialogUtil = AllDialogUtil.getInstance(this);
        String okText = this.getString(R.string.label_btn_ok);
        String cancelText = this.getString(R.string.cancel);
        String notice = "是否删除此工资单?";
        mDialogUtil.titleDeleteMsgStyle(okText, cancelText, notice);
        mDialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {
            @Override
            public void onConfirmClick(Dialog lDialog) {
                lDialog.dismiss();
                deleteOneSalaryForm(salary_list_id);
            }

            @Override
            public void onCancelClick(Dialog lDialog) {
                lDialog.dismiss();
            }
        });
    }
}
